﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using AjaxControlToolkit;

namespace MinhTuan
{   //http://www.beansoftware.com/ASP.NET-Source-Code/Media-Player-VB.NET.aspx
    //http://www.beansoftware.com/ASP.NET-Tutorials/Custom-Error-Pages.aspx

    public class Access
    {
        #region Variables

        private static SqlConnection mySqlConnection = new SqlConnection();
        public static SqlCommand mySqlCommand = new SqlCommand();
       
        private static String[] myTitle = {"www.vienthongsaigon.com | Cung cap the nho, dau doc, tai nghe bluetooth, linh kien, phu kien dien thoai...",
                                   "www.thegioidienthoai.com | Cung cap the nho, the nho chinh hang, the nho gia re, nha phan phoi the nho...",
                                   "www.thegioidienthoai.com | Tai nghe bluetooth, memory, flash card, dau doc, phu kien, loa thoi trang, linh kien dien thoai...",
                                   "www.thegioidienthoai.com | The nho, USB Transend, Kingston, Kingmax, Sony, Tai nghe Bluetooth, sac dien thoai, sac xe hoi...",
                                   "www.thegioidienthoai.com | Tin tuc vien thong, tin tuc cong nghe, tin khuyen mai, cap nhat tin tuc 24/7, tin thoi tiet...", };

        private static String[] myKeywords = {"Điện thoại,máy tính xách tay, phụ kiện, phụ kiện lap top, phụ kiện điện thoại",
                                              "dien thoai, lap top, phu kien, phu kien đien thoai, phu kien laptop",
                                              "điện thoại chính hãng, laptop chính hãng, phụ kiện chất lượng",
                                              "thế giới số,tin tức công nghệ, phần mềm số, phạm minh tuấn, pham minh tuan"};

        #endregion

        #region DataAccess

        public Access()
        {
            mySqlConnection = new SqlConnection("Data Source=admin-pc\\sqlexpress;Initial Catalog=Dienthoai;Integrated Security=True");
            //mySqlConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["connectstring"]);
        }

        public static void InitAccess()
        {
            //try
            //{
            mySqlConnection = new SqlConnection("Data Source=admin-pc\\sqlexpress;Initial Catalog=Dienthoai;Integrated Security=True");
                if (mySqlConnection.State != System.Data.ConnectionState.Open)
                {
                    mySqlConnection.Open();
                }
                mySqlCommand = new SqlCommand();
                mySqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
                mySqlCommand.Connection = mySqlConnection;
            //}
            //catch (System.Exception)
            //{

            //}
        }

        public static void ClosedAccess()
        {
            try
            {
                if (mySqlConnection.State != System.Data.ConnectionState.Closed)
                {
                    mySqlConnection.Close();
                }
                if (mySqlCommand != null)
                {
                    mySqlCommand.Dispose();
                }
            }

            catch (System.Exception)
            {
            }
        }
        public static DataTable GetData(string Storeproduce)
        {
            DataTable tbl = new DataTable();
            try {
                Access.InitAccess();
                Access.mySqlCommand.CommandText = Storeproduce;
                SqlDataAdapter da = new SqlDataAdapter(Access.mySqlCommand);
                DataSet ds = new DataSet();
                da.Fill(ds);
                tbl = ds.Tables[0];
                Access.ClosedAccess();
                return tbl;
            }
            catch {
                Access.ClosedAccess();
                return tbl;
            }
        }
        public static DataTable GetData(string Storeproduce,string Parameter,int ValueParameter,SqlDbType Sqltype)
        {
            DataTable tbl = new DataTable();
            try
            {
                Access.InitAccess();
                Access.mySqlCommand.CommandText = Storeproduce;
                Access.mySqlCommand.Parameters.Add(Parameter,Sqltype).Value=ValueParameter;
                SqlDataAdapter da = new SqlDataAdapter(Access.mySqlCommand);
                DataSet ds = new DataSet();
                da.Fill(ds);
                tbl = ds.Tables[0];
                Access.ClosedAccess();
                return tbl;
            }
            catch
            {
                Access.ClosedAccess();
                return tbl;
            }
        }
        #endregion

        #region Contact

        public static void GetContact(ListView Contacts, String Lang_Id)
        {
            try
            {
                Access.InitAccess();
                Access.mySqlCommand.CommandText = "Sp_GetContact";
                Access.mySqlCommand.Parameters.Add("@Lang_Id", SqlDbType.NChar).Value = Lang_Id;
                DataSet myDataset = new DataSet();
                SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(Access.mySqlCommand);
                mySqlDataAdapter.Fill(myDataset, "GetContact");
                Access.ClosedAccess();
                Contacts.DataSource = myDataset.Tables[0];
                Contacts.DataBind();
            }
            catch (System.Exception)
            {
                Access.ClosedAccess();
                Contacts.DataSource = new DataTable("GetContact");
                Contacts.DataBind();
            }
        }

        public static Boolean SendContact(String Name, String Email, String Title, String Summary, String Lang)
        {
            try
            {
                String Time = DateTime.Now.ToString("hh : mm dd/MM/yyyy");
                Access.InitAccess();
                Access.mySqlCommand.CommandText = "Sp_AddContact";
                Access.mySqlCommand.Parameters.Add("@Item_Name", SqlDbType.NVarChar).Value = Name;
                Access.mySqlCommand.Parameters.Add("@Item_Email", SqlDbType.NVarChar).Value = Email;
                Access.mySqlCommand.Parameters.Add("@Item_Title", SqlDbType.NVarChar).Value = Title;
                Access.mySqlCommand.Parameters.Add("@Item_Summary", SqlDbType.NVarChar).Value = Summary;
                Access.mySqlCommand.Parameters.Add("@Item_Time", SqlDbType.NVarChar).Value = Time;
                Access.mySqlCommand.Parameters.Add("@Lang_Id", SqlDbType.NChar).Value = Lang;
                Int16 Result = Convert.ToInt16(Access.mySqlCommand.ExecuteNonQuery());
                Access.ClosedAccess();
                if (Result == 1)
                {
                    return true;
                }
                return false;
            }
            catch (System.Exception)
            {
                Access.ClosedAccess();
                return false;
            }
        }

        #endregion

        #region Polls

        public static void SetVote(Int32 OptionId)
        {
            try
            {
                Access.InitAccess();
                Access.mySqlCommand.CommandText = "Sp_SetVote";
                Access.mySqlCommand.Parameters.Add("@Option_Id", SqlDbType.SmallInt).Value = OptionId;
                Access.mySqlCommand.ExecuteNonQuery();
                Access.ClosedAccess();
            }
            catch (System.Exception)
            {
                Access.ClosedAccess();
            }
        }
    
        
        #endregion

        #region Other tools

        public static Byte[] SecurityTool(String Source)
        {
            Byte[] GetSource = Encoding.UTF8.GetBytes(Source);
            MD5 myMD5 = new MD5CryptoServiceProvider();
            Byte[] Result = myMD5.ComputeHash(GetSource);
            return Result;
        }

        public static String MakeCapcha()
        {
            String Result = ""; String Temp = "ABCDEFGHIKLMNOPQRSTUVWXYZ123456789";  
            Random myRand = new Random();
            for (Int16 index = 0; index <= 4; index++)
            {
                Result += Temp[myRand.Next(0, 33)];
            }
            return Result;
        }
        public static void UpdatePage(System.Web.UI.HtmlControls.HtmlHead myPageHeader)
        {
            try
            {
                Random myRand =  new Random();

                myPageHeader.Title = "Thế giới điện thoại | dien thoa, laptop, phu kien ";
                HtmlMeta metaValue = new HtmlMeta();

                // keywords meta tag
                metaValue.Attributes.Add("name", "keywords");
                metaValue.Attributes.Add("content", myKeywords[myRand.Next(0, 3)]);
                myPageHeader.Controls.Add(metaValue);

                // description meta tag
                metaValue = new HtmlMeta();
                metaValue.Attributes.Add("name", "description");
                metaValue.Attributes.Add("content", myKeywords[myRand.Next(0, 3)]);
                myPageHeader.Controls.Add(metaValue);

                // author meta tag
                metaValue = new HtmlMeta();
                metaValue.Attributes.Add("name", "author");
                metaValue.Attributes.Add("content", "Pham Minh Tuan - pminhtuan89@gmail.com");
                myPageHeader.Controls.Add(metaValue);

            }
            catch (Exception)
            {
            }
        }
        public static void UpdatePage(System.Web.UI.HtmlControls.HtmlHead myPageHeader, String Title, String Keyword)
        {
            try
            {
                myPageHeader.Title = "The Gioi Dien Thoai | " + Title;
                HtmlMeta metaValue = new HtmlMeta();

                // keywords meta tag
                metaValue.Attributes.Add("name", "keywords");
                metaValue.Attributes.Add("content", Keyword +", " + myKeywords[3]);
                myPageHeader.Controls.Add(metaValue);

                // description meta tag 
                metaValue = new HtmlMeta();
                metaValue.Attributes.Add("name", "description");
                metaValue.Attributes.Add("content", Keyword + ", " + myKeywords[3]);
                myPageHeader.Controls.Add(metaValue);

                // author meta tag
                metaValue = new HtmlMeta();
                metaValue.Attributes.Add("name", "author");
                metaValue.Attributes.Add("content", "Pham minh tuan - pminhtuan89@gmail.com");
                myPageHeader.Controls.Add(metaValue);

                //copyright meta tag
                metaValue = new HtmlMeta();
                metaValue.Attributes.Add("name", "copyright");
                metaValue.Attributes.Add("content", "Copyright");
                myPageHeader.Controls.Add(metaValue);

                // owner meta tag
                metaValue = new HtmlMeta();
                metaValue.Attributes.Add("name", "owner");
                metaValue.Attributes.Add("content", "Cong ty TNHH ");
                myPageHeader.Controls.Add(metaValue);

                // robots meta tag
                metaValue = new HtmlMeta();
                metaValue.Attributes.Add("name", "robots");
                metaValue.Attributes.Add("content", "Index, follow, All");
                myPageHeader.Controls.Add(metaValue);

                // revisit meta tag
                metaValue = new HtmlMeta();
                metaValue.Attributes.Add("name", "revisit-after");
                metaValue.Attributes.Add("content", "1 day");
                myPageHeader.Controls.Add(metaValue);

                // yahoo meta tag
                metaValue = new HtmlMeta();
                metaValue.Attributes.Add("name", "y_key");
                metaValue.Attributes.Add("content", "dc0108e625922106");
                myPageHeader.Controls.Add(metaValue);

                // google meta tag
                metaValue = new HtmlMeta();
                metaValue.Attributes.Add("name", "google-site-verification");
                metaValue.Attributes.Add("content", "jqcs8sfyEBGXYGfKLjOCHbhBfn29hE6kMlEe1HF6_4M");
                myPageHeader.Controls.Add(metaValue);
            }
            catch (Exception)
            {
            }
        }

        public static String RemoveVietnameseSigns(String VietnameseSource)
        {
            VietnameseSource = VietnameseSource.Trim();
            try
            {

                String[] SpecialSign = { ":", "-", "- ", " -", "'", ";", ",", ".", "\"", "?", "“", "”" };
                for (Int16 index = 0; index < SpecialSign.Length; index++)
                {
                    VietnameseSource = VietnameseSource.Replace(SpecialSign[index], "");
                }
                String[] VietnameseSigns = new String[] { "aAeEoOuUiIdDyY", "áàạảãâấầậẩẫăắằặẳẵ", "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ", "éèẹẻẽêếềệểễ", "ÉÈẸẺẼÊẾỀỆỂỄ", "óòọỏõôốồộổỗơớờợởỡ", "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ", "úùụủũưứừựửữ", "ÚÙỤỦŨƯỨỪỰỬỮ", "íìịỉĩ", "ÍÌỊỈĨ", "đ", "Đ", "ýỳỵỷỹ", "ÝỲỴỶỸ" };
                for (int i = 1; i < VietnameseSigns.Length; i++)
                {
                    for (int j = 0; j < VietnameseSigns[i].Length; j++)
                    {
                        VietnameseSource = VietnameseSource.Replace(VietnameseSigns[i][j], VietnameseSigns[0][i - 1]);
                    }
                }
                VietnameseSource = VietnameseSource.Replace(" ", "-");
                return VietnameseSource;
            }
            catch (Exception)
            {
                return VietnameseSource;
            }
        }

        public static String GetDateString(String Lang)
        {
            Int16 day = Convert.ToInt16(System.DateTime.Now.DayOfWeek);
            if (String.Compare(Lang, "vn", true) == 0)
            {
                String[] myVnDate = { "Chủ nhật, ngày ", "Thứ 2, ngày ", "Thứ 3, ngày ", "Thứ 4, ngày ", "Thứ năm, ngày ", "Thứ 6, ngày ", "Thứ 7, ngày " };
                return myVnDate[day] + System.DateTime.Now.ToString("dd/MM/yyyy HH:mm") + " GMT+7";
            }
            else
            {
                String[] myEnDate = { "Sunday, ", "Monday, ", "Tuesday, ", "Wednesday, ", "Thursday, ", "Friday, ", "Saturday, " };
                return myEnDate[day] + System.DateTime.Now.ToString("dd/MM/yyyy HH:mm") + " GMT+7";
            }
        }

        public static String GetDateString(DateTime myDate, String Lang)
        {
            Int16 day = Convert.ToInt16(myDate.DayOfWeek);
            if (String.Compare(Lang, "vn", true) == 0)
            {
                String[] myVnDate = { "Chủ nhật, ngày ", "Thứ 2, ngày ", "Thứ 3, ngày ", "Thứ 4, ngày ", "Thứ năm, ngày ", "Thứ 6, ngày ", "Thứ 7, ngày " };
                return myVnDate[day] + System.DateTime.Now.ToString("dd/MM/yyyy HH:mm") + " GMT+7";
            }
            else
            {
                String[] myEnDate = { "Sunday, ", "Monday, ", "Tuesday, ", "Wednesday, ", "Thursday, ", "Friday, ", "Saturday, " };
                return myEnDate[day] + System.DateTime.Now.ToString("dd/MM/yyyy HH:mm") + " GMT+7";
            }
        }


       #endregion

        #region Focus

        //public static AjaxControlToolkit.Slide[] GetFocusItems(String Lang)
        //{ 
        //    AjaxControlToolkit.Slide[] slides = new AjaxControlToolkit.Slide[0];
        //    try
        //    {
        //        Access.InitAccess();
        //        Access.mySqlCommand.CommandText = "Sp_GetFocusItem";
        //        Access.mySqlCommand.Parameters.Add("@Lang_Id", SqlDbType.NChar).Value = Lang;
        //        SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(Access.mySqlCommand);
        //        DataSet myDataset = new DataSet();
        //        mySqlDataAdapter.Fill(myDataset, "FocusItem");
        //        DataTable myDataTable = myDataset.Tables[0];
        //        if (myDataTable.Rows.Count > 0) 
        //        {
        //            slides = new AjaxControlToolkit.Slide[myDataTable.Rows.Count];
        //            DataRow myRow;
        //            for (Int16 index = 0; index < myDataTable.Rows.Count; index ++ )
        //            {
        //                myRow = myDataTable.Rows[index];
        //                slides[index] = new AjaxControlToolkit.Slide("http://localhost:13101/Vienthongsaigon/" + myRow["Focus_Image"].ToString(), myRow["Focus_Name"].ToString(), myRow["Focus_Summary"].ToString());
        //            }
        //        }
        //        Access.ClosedAccess();
        //        return slides;
        //    }
        //    catch (System.Exception)
        //    {
        //        Access.ClosedAccess();
        //        return new AjaxControlToolkit.Slide[0];
        //    }
        //}
        
        #endregion

    }
}